<!-- 用户界面 -->
<template>
  <div class="box">
      <!-- 面包屑 -->
     <el-breadcrumb separator="/" class="header">
        <el-breadcrumb-item >首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
     </el-breadcrumb>

     <!-- 搜索 -->
     <el-input v-model="query" placeholder="请输入内容" class="input"></el-input>
     <el-button icon="el-icon-search" @click="look"></el-button>
     <el-button type="success" plain  @click="dialogVisible = true">添加用户</el-button>

     <!-- 表格  一行代表一个用户-->
        <el-table
         class="table"
        :data="tableData"
        style="width: 100%">
          <el-table-column
            prop="id"
            label="id"
            width="60">
        </el-table-column>
          <el-table-column
            prop="username"
            label="姓名"
            width="180">
        </el-table-column>
          <el-table-column
            prop="email"
            label="邮箱"
            width="180">
        </el-table-column>
        <el-table-column
            prop="mobile"
            label="电话"
            width="180">
        </el-table-column>
        <el-table-column
            prop="create_time"
            label="创建日期"
            width="180">
          <!-- 不是字符串要用template -->
            <template v-slot="tableData">
              {{tableData.row.create_time | fmtdate}}
            </template>
            
        </el-table-column>
        <el-table-column
            prop="mg_state"
            label="用户状态">
            <template>
              <el-switch
                v-model="value"
                active-color="#13ce66"
                inactive-color="#ff4949"
               >
              </el-switch>
            </template>
        </el-table-column>
         <el-table-column
            prop="id"
            label="操作">
            <template v-slot="tableData">
               <el-button type="primary" icon="el-icon-edit" circle @click="editclick(tableData.row.id)"></el-button>
               <el-button type="success" icon="el-icon-check" circle @click="roleclick(tableData.row)"></el-button>
               <el-button type="danger"   icon="el-icon-delete" circle  @click="deleteclick(tableData.row.id)"></el-button>
            </template>
        </el-table-column>
    </el-table>

    <!-- 分页 -->
      <el-pagination
      class="fen"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="1"
      :page-sizes="[2,4]"
      :page-size="2"
      layout="total, sizes, prev, pager, next, jumper"
      :total="32">
      </el-pagination>

          

      <!-- 添加用户的弹框 -->
      <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose">
       <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="用户名称" prop="username"
           :rules="[
              { required: true, message: '年龄不能为空'},
            ]"
          >
            <el-input v-model="form.username"></el-input>
          </el-form-item>
           <el-form-item label="用户密码"  prop="password"
            :rules="[
              { required: true, message: '年龄不能为空'},
            ]"
           >
            <el-input v-model="form.password"></el-input>
          </el-form-item>
           <el-form-item label="邮箱"  prop="email"
            :rules="[
            ]"
           >
            <el-input v-model="form.email"></el-input>
          </el-form-item>
           <el-form-item label="手机号" prop="mobile"
             :rules="[
              { type: 'number', message: '年龄必须为数字值'}
            ]">
            <el-input v-model.number="form.mobile"></el-input>
          </el-form-item>
       </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="adduser">确 定</el-button>
      </span>
    </el-dialog>


  <!-- 分配角色的弹框 -->
 <el-dialog
      title="提示"
      :visible.sync="dialogVisiblerole"
      width="50%"
      :before-close="handleClose">
       <el-form ref="form" :model="roleform" label-width="80px">
            <h3>用户名:{{roleform.username}}</h3>
          <el-form-item label="角色" prop="username">
              <el-select v-model="pickrole" placeholder="请选择">
                 <el-option
                      v-for="(item,index) in options"
                      :key="item.value"
                      :label="item.roleName"
                      :value="index">
                    </el-option>
              </el-select>
            </el-form-item>
       </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisiblerole = false">取 消</el-button>
        <el-button type="primary" @click="role" >确 定</el-button>
      </span>
    </el-dialog>







    <!-- 用户编辑的弹框 -->
      <el-dialog
      title="提示"
      :visible.sync="dialogVisible1"
      width="50%"
      :before-close="handleClose">
       <el-form ref="editform" :model="editform" label-width="80px">
          <el-form-item label="用户新id" prop="newid"
           :rules="[
              { required: true, message: 'id不能为空'},
            ]"
          >
            <el-input v-model.number="editform.newid"></el-input>
          </el-form-item>
           <el-form-item label="邮件" prop="email">
            <el-input v-model="editform.email"></el-input>
          </el-form-item>
           <el-form-item label="手机号" prop="mobile">
            <el-input v-model.number="editform.mobile"></el-input>
          </el-form-item>
       </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="eidt">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {getuserlist,getuserid,getusername,deleteuser,edituser,getroles,setrole} from "network/user.js"
export default {
  name:"user",
  data () {
     return {
         value: true,
         query:"",
         pagenum:1,
         pagesize:2,
         tableData: [],
         dialogVisible: false,
         dialogVisible1: false,
         dialogVisiblerole: false,
         form:{
           username:"",
           password:"",
           email:"",
           mobile:""
         },
         editform:{
            id:"",
            newid:"",
            email:"",
            mobile:""
         },
        //  角色数据
        pickrole:"",
        options:[],
        roleform:{
            id:"",
            username:"",
            rid:""
        },
        roleid:""
     }
  },
  //该钩子函数执行里面的方法 methods还没有初始化完成
  created (){
      getuserlist(this.query,this.pagenum,this.pagesize).then(res=>{
                this.tableData=res.data.data.users
                 console.log(this.tableData)
       })
  },
  methods: {
    
    //弹框关闭
    handleClose(done) {
       //把输入过的内容重置
        for(var k in this.form){
             this.form[k]=""
        }
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
      //查询
      look(){
          console.log(this.query)
          this.tableData=[]
          this.getuserlist()
      },
      //调用数据方法
      getuserlist(){
          getuserlist(this.query,this.pagenum,this.pagesize).then(res=>{
                this.tableData=res.data.data.users
                 console.log(this.tableData)
       })
      },

      handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
      },
      //这是点击的页码 val就是当前点击的页码
      handleCurrentChange(val) {
         this.pagenum=val
         //清空数组
         this.tableData.length=0
         this.getuserlist()
      },
      //添加用户
      adduser(){
         this.dialogVisible = false 
         this.$refs.form.validate((valid) => {
          if (valid) {
            // 当用户填写的内容都满足时  发送请求
            getusername(this.form).then(res=>{
            })
           this.$message({
              message: '添加成功',
              type: 'success'
            });
          // this.$http.defaults.headers['Authorization'] = localStorage.getItem("token")
          // this.$http.post("https://www.liulongbin.top:8888/api/private/v1/users",this.form).then(res=>{
          //         console.log(res)
          // })
          } else {
            this.$message.error('添加失败');
            return false;
          }
           // 添加完成后更新数据
            this.tableData.length=0
            setTimeout(() => {
                 this.getuserlist()
                 console.log(111111111)
            }, 1000);
         
          });
      },
      //删除操作
      deleteclick(id){
        deleteuser(parseInt(id)).then(res=>{
          console.log(res)
          if(res.data.meta.status==200){
              this.$message.success("删除成功")
              // 删除成功后  更新数据
              this.tableData.length=0
              setTimeout(() => {
                 this.getuserlist()
              }, 500);
             
          }else{
                this.$message.error('删除失败');
          }
        })
      },

      //编辑操作
      editclick(id){
         this.dialogVisible1 = true
         this.editform.id=id
         console.log( this.editform.id)
      },
      eidt(){
            edituser(this.editform.id,this.editform).then(res=>{
              console.log(res)
            })
             this.dialogVisible1 = false
            //  编辑完成后更新数据
            this.tableData.length=0
            this.getuserlist()
        },
      
      //分配角色
      roleclick(row){
        //点击分配按钮 获取用户名和id
        this.dialogVisiblerole=true
        //在弹框中显示用户名
        this.roleform.username=row.username
        this.roleform.id=row.id
        //获取角色列表 展现在弹框页面中
          getroles().then(res=>{
            this.options.push(...res.data.data)
            console.log(this.options)
          })
      },
      
      role(row){
        //设置用户的权限角色 用来实现权限功能的分配  
          setrole( this.roleform.id,this.options[this.pickrole].id).then(res=>{
                 console.log(res)
          })
          this.pickrole=""
          this.options.length=0
          this.dialogVisiblerole = false
      }
    }


}

</script>
<style  scoped>
        .box{
             background: white;
             overflow: hidden;
        }
        .header{
            margin-left: 15px;
            font-size: 17px;
        }
        .input{
            margin-left: 15px;
            margin-top: 10px;
            width: 300px;
        }
        .table{
               margin-left: 25px;
        }
        .fen{
         margin-left: 25px;
        }
        h3{
           margin-left: 38px;
        }
</style>